package acwing._2LanQiao._9DP;
/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._2LanQiao._9DP
 * @File : Q1303.java
 * @Author : WangRuoyu
 * @Date 2023/3/23 22:42
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Q1303 {

    static void mul(long[] f, long[][] a, long m) {
        long[] tmp = new long[f.length];
        for (int i = 0; i < tmp.length; ++i) {
            for (int j = 0; j < tmp.length; ++j) {
                tmp[i] = (tmp[i] + f[j] * a[j][i]) % m;
            }
        }
        System.arraycopy(tmp, 0, f, 0, f.length);
    }

    static void mul(long[][] a, long m) {
        long[][] tmp = new long[a.length][a.length];
        for (int i = 0; i < a.length; ++i) {
            for (int j = 0; j < a.length; ++j) {
                for (int k = 0; k < a.length; ++k) {
                    tmp[i][j] = (tmp[i][j] + a[i][k] * a[k][j]) % m;
                }
            }
        }
        for (int i = 0; i < a.length; ++i) {
            System.arraycopy(tmp[i], 0, a[i], 0, a.length);
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] words = br.readLine().split(" ");

        long n = Long.parseLong(words[0]);
        long m = Long.parseLong(words[1]);

        long[][] a = {{0, 1, 0}, {1, 1, 1}, {0, 0, 1}};
        long[] f1 = {1, 1, 1};

        n--;
        while (n > 0) {
            if ((n & 1) == 1) mul(f1, a, m);
            n >>= 1;
            mul(a, m);
        }
        System.out.println(f1[2]);
    }
}
